package daily.year2024.m10;

import junior.链表.ListNode;

public class d31 {
    //解法：快慢指针
    public class Solution {
        public ListNode detectCycle(ListNode head) {
            ListNode quick = head, slow = head;
            while(quick != null && quick.next != null) {
                slow = slow.next;
                quick = quick.next.next;
                if(quick == slow) {
                    break;
                }
            }
            if(quick == null || quick.next == null) {
                return null;
            }
            ListNode ptr = head;
            while(ptr != slow) {
                ptr = ptr.next;
                slow = slow.next;
            }
            return ptr;
        }
    }
}
